package net.zoneland.knowledge.dao;

import net.zoneland.knowledge.model.SharedEditingFolderInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface SharedEditingFolderMapper {

    /**
     * 根据文件名查询共享编辑文件夹信息.
     * @param status 文件状态.
     * @param fileName 文件名.
     * @param fileType 文件类型.
     * @return 共享编辑文件/文件夹信息.
     */
    @Select({
            "SELECT ID, FILE_ID, FILE_NAME, FILE_TYPE, PARENT_ID, ROOT_ID, PATH_ID, PATH_NAME, ",
            " EXTEND, CHILDREN, CREATE_TIME, UPDATE_TIME, STATUS FROM (",
            " select ID, FILE_ID, FILE_NAME, FILE_TYPE, PARENT_ID, ROOT_ID, PATH_ID, PATH_NAME, ",
            " EXTEND, CHILDREN, CREATE_TIME, UPDATE_TIME, STATUS ",
            " FROM AITK_SHARED_EDITING_FOLDER ",
            " WHERE STATUS=#{status,jdbcType=BOOLEAN} AND FILE_NAME=#{fileName,jdbcType=VARCHAR} ",
            " AND FILE_TYPE=#{fileType,jdbcType=VARCHAR} ORDER BY UPDATE_TIME DESC ",
            ") WHERE ROWNUM = 1",
    })
    @Results({
            @Result(column="ID", property="id", jdbcType= JdbcType.VARCHAR, id=true),
            @Result(column="FILE_ID", property="fileId", jdbcType=JdbcType.VARCHAR),
            @Result(column="FILE_NAME", property="fileName", jdbcType=JdbcType.VARCHAR),
            @Result(column="FILE_TYPE", property="fileType", jdbcType=JdbcType.VARCHAR),
            @Result(column="PARENT_ID", property="parentId", jdbcType=JdbcType.VARCHAR),
            @Result(column="ROOT_ID", property="rootId", jdbcType=JdbcType.VARCHAR),
            @Result(column="PATH_ID", property="pathId", jdbcType=JdbcType.VARCHAR),
            @Result(column="PATH_NAME", property="pathName", jdbcType=JdbcType.VARCHAR),
            @Result(column="EXTEND", property="extend", jdbcType=JdbcType.VARCHAR),
            @Result(column="CHILDREN", property="children", jdbcType=JdbcType.VARCHAR),
            @Result(column="CREATE_TIME", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="UPDATE_TIME", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="STATUS", property="status", jdbcType=JdbcType.BOOLEAN)
    })
    SharedEditingFolderInfo findSharedEditingFolderInfo(@Param("status") final boolean status,
           @Param("fileName") final String fileName, @Param("fileType") final String fileType);

    /**
     * 根据文件名查询共享编辑文件夹是否存在.
     * @param status 文件状态.
     * @param fileName 文件名.
     * @param fileType 文件类型.
     * @return 是否存在true false.
     */
    @Select({
            "SELECT ",
            "CASE ",
            "WHEN COUNT(1) > 0 THEN 1 ",
            "ELSE 0 ",
            "END AS result ",
            "FROM AITK_SHARED_EDITING_FOLDER ",
            "WHERE STATUS=#{status,jdbcType=BOOLEAN} AND FILE_NAME=#{fileName,jdbcType=VARCHAR} ",
            "AND FILE_TYPE=#{fileType,jdbcType=VARCHAR}"
    })
    boolean isFileNameExist(@Param("status") final boolean status,
           @Param("fileName") final String fileName, @Param("fileType") final String fileType);


    /**
     * 新增文件/文件夹信息.
     * @param info  文件/文件夹信息
     */
    @Insert({
            "insert into AITK_SHARED_EDITING_FOLDER (ID, FILE_ID, FILE_NAME, FILE_TYPE, ",
            " PARENT_ID, ROOT_ID, PATH_NAME, PATH_ID, EXTEND, CHILDREN, STATUS )",
            " values ",
            "(#{id,jdbcType=VARCHAR}, #{fileId,jdbcType=VARCHAR}, #{fileName,jdbcType=VARCHAR}, ",
            " #{fileType,jdbcType=VARCHAR}, #{parentId,jdbcType=VARCHAR}, #{rootId,jdbcType=VARCHAR}, ",
            " #{pathName,jdbcType=VARCHAR}, #{pathId,jdbcType=VARCHAR}, #{extend,jdbcType=VARCHAR}, ",
            " #{children,jdbcType=VARCHAR}, #{status,jdbcType=BOOLEAN})"
    })
    void insertFolder(final SharedEditingFolderInfo info);

    /**
     * 更新文件/文件夹信息.
     * @param info 文件/文件夹信息.
     * @return 返回更新条数.
     */
    @Update({
            "update AITK_SHARED_EDITING_FOLDER ",
            "set FILE_NAME = #{fileName,jdbcType=VARCHAR}, ",
            "FILE_ID = #{fileId,jdbcType=VARCHAR}, ",
            "FILE_TYPE = #{fileType,jdbcType=VARCHAR}, ",
            "PARENT_ID = #{parentId,jdbcType=VARCHAR}, ",
            "ROOT_ID = #{rootId,jdbcType=VARCHAR}, ",
            "EXTEND = #{extend,jdbcType=VARCHAR} ",
            "where ID = #{id,jdbcType=VARCHAR}"
    })
    int updateFolder(final SharedEditingFolderInfo info);

}
